package com.freeman.cement.service.impl;

import com.freeman.cement.entity.hikvision.EventElement;
import com.freeman.cement.service.EventAnalysisService;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.JsonPathException;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static org.apache.commons.lang3.StringEscapeUtils.unescapeJava;

//使用那种解析就在这个实现类上加注解
//@Service
public class EventAnalysisServiceImplV1 implements EventAnalysisService {

    private String jsonPathAnalysis(String text, String pattern) {
        Object obj = null;
        try {
            obj = JsonPath.read(text, pattern);
        } catch (JsonPathException je) {
            System.out.println("JsonPath解析" + pattern + "出错");
            je.printStackTrace();
        } catch (Exception e) {
            System.out.println("解析" + pattern + "出错");
            e.printStackTrace();
        } finally {
            if (obj != null)
                return obj.toString();
            else
                return "";
        }
    }

    @Override
    public List<EventElement> analysisEvent(String params) {
        /**
         * 报文解析是要解析出
         * 1、eventId 事件Id
         * 2、eventType 事件类型
         * 3、happenTime 发生事件
         * 4、cameraIndexCode 摄像头编号
         * 5、imageUrl 图片地址
         *
         * 下面解析方法要求:
         * 1、events必须为Events[]数组
         */

        List<EventElement> list = new ArrayList<>();
        if (StringUtils.contains(params, "events")) {
            //Events[]是数组
            List<String> events = null;
            try {
                events = JsonPath.read(params, "$.events");
            } catch (Exception e) {
                System.out.println("报文包含events，但不可被解析为events数组，解析出错");
                e.printStackTrace();
                return null;
            }
            if (events != null && events.size() > 0) {
                for (int i = 0; i < events.size(); i++) {
                    EventElement element = new EventElement();
                    //事件Id
                    String eventId = StringUtils.contains(params, "eventId") ? jsonPathAnalysis(params, "$.events[" + Integer.toString(i) + "].eventId") : "";
                    element.setEventId(eventId);
                    //事件类型
                    String eventType = StringUtils.contains(params, "eventType") ? jsonPathAnalysis(params, "$.events[" + Integer.toString(i) + "].eventType") : "";
                    element.setEventType(eventType);
                    //发生时间
                    String happenTime = StringUtils.contains(params, "happenTime") ? jsonPathAnalysis(params, "$.events[" + Integer.toString(i) + "].happenTime") : (new Date()).toString();
                    element.setHappenTime(happenTime);
                    //摄像头编号
                    String cameraIndexCode = StringUtils.contains(params, "cameraIndexCode") ? jsonPathAnalysis(params, "$.events[" + Integer.toString(i) + "].cameraIndexCode") : "";
                    //如果没有cameraIndexCode，查找下是否有srcIndex，如果此时有srcIndex，将srcIndex作为cameraIndexCode
                    if (StringUtils.isBlank(cameraIndexCode)) {
                        cameraIndexCode = StringUtils.contains(params, "srcIndex") ? jsonPathAnalysis(params, "$.events[" + Integer.toString(i) + "].srcIndex") : "";
                    }
                    element.setSrcIndex(cameraIndexCode);
                    //报警图片
                    String imageUrl = "";
                    if (StringUtils.contains(params, "imageUrl")) {
                        //imageUrl通过深层扫描找到
                        //"["http:\/\/10.3.1.12:8080\/xxx"]"
                        imageUrl = StringUtils.contains(params, "imageUrl") ? jsonPathAnalysis(params, "$.events[" + Integer.toString(i) + "]..imageUrl") : "";
                        imageUrl = imageUrl.replace("[", "");
                        imageUrl = imageUrl.replace("]", "");
                        imageUrl = imageUrl.replace("\"", "");
                        //去掉转义字符
                        imageUrl = unescapeJava(imageUrl);
                        imageUrl = StringUtils.deleteWhitespace(imageUrl);
                    } else
                        imageUrl = "";
                    element.setImageUrl(imageUrl);
                    if(StringUtils.isNotBlank(eventId))
                        list.add(element);
                }
                return list;
            }
        }
        return null;
    }

    @Override
    public List<EventElement> analysisEventTest(String params) {
        return analysisEvent(params);
    }

}